package com.fowo.api.syncAmazon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.common.model.RException;
import com.fowo.api.entity.FbaShipment;
import com.fowo.api.entity.TmpFbaShipment;
import com.fowo.api.mapper.FbaShipmentMapper;
import com.fowo.api.mapper.TmpFbaShipmentMapper;
import com.fowo.api.service.FbaShipmentService;
import com.fowo.api.service.impl.FbaShipmentServiceImpl;
import com.fowo.api.syncAmazon.mapper.ShipmentMapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class TmpFbaShipmentAmazonServiceImpl extends ServiceImpl<TmpFbaShipmentMapper, TmpFbaShipment> {

    @Resource
    private ShipmentMapper shipmentMapper;
    @Resource
    private FbaShipmentServiceImpl fbaShipmentService;
    @Resource
    private TmpFbaShipmentMapper tmpFbaShipmentMapper;

    private final IdentifierGenerator identifierGenerator = new DefaultIdentifierGenerator();

    @Transactional(rollbackFor = Exception.class)
    public void save(List<TmpFbaShipment> list){
        if(list == null || list.size() < 1){
            throw new RException("FBA货架数据为空");
        }
        List<List<TmpFbaShipment>> partition = Lists.partition(list, 200);
        List<FbaShipment> shipmentList = new ArrayList<>();
        List<FbaShipment> updateList = new ArrayList<>();
        //同步到业务表 取出表中所有亚马逊数据 判断新增或者修改
        List<String> allShipmentId = shipmentMapper.getAllShipmentId();
        for (List<TmpFbaShipment> shipments:partition) {
            if(this.saveBatch(shipments)){
                for (TmpFbaShipment shipment:shipments) {
                    FbaShipment fbaShipment = new FbaShipment();
                    fbaShipment.setId((Long) identifierGenerator.nextId(null));
                    fbaShipment.setShipmentId(shipment.getShipmentId());
                    fbaShipment.setShipmentName(shipment.getShipmentName());
                    fbaShipment.setStreetAddress(shipment.getAddressLine());
                    fbaShipment.setCity(shipment.getCity());
                    fbaShipment.setCountry(shipment.getDistrictOrCounty());
                    fbaShipment.setPostcode(shipment.getPostalCode());
                    fbaShipment.setDestinationFulfillmentCenterId(shipment.getDestinationFulfillmentCenterId());
                    fbaShipment.setShipmentStatus(shipment.getShipmentStatus());
                    fbaShipment.setLabelType(shipment.getLabelPrepType());
                    fbaShipment.setIsSynchronous("1");
                    if(allShipmentId.contains(shipment.getShipmentId())){
                        fbaShipment.setLastUpdateTime(new Date());
                        updateList.add(fbaShipment);
                    }else {
                        fbaShipment.setCreateTime(new Date());
                        shipmentList.add(fbaShipment);
                    }
                }
                fbaShipmentService.saveBatch(shipmentList);
//                shipmentMapper.insertShipment(shipmentList);
                shipmentMapper.updateShipment(updateList);
                tmpFbaShipmentMapper.delete(new QueryWrapper<>());
            }
        }
    }

}
